/*
# Rouleur Log - A log for cyclists.
# Copyright (C) 2006  Edward Copony (ecopony@gmail.com)
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.blazingredfireball.rouleurlog.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.business.RideDelegate;
	import com.blazingredfireball.rouleurlog.model.RouleurlogModelLocator;
	import com.blazingredfireball.rouleurlog.vo.RideVO;
	import com.blazingredfireball.rouleurlog.util.ValueObjectUtil;
	
	import flash.events.SQLEvent;
	import flash.data.SQLResult;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	public class GetRidesCommand extends BaseCommand implements ICommand, IResponder
	{
		private var model : RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		
		public function execute(event:CairngormEvent):void
		{
		  var delegate : RideDelegate = new RideDelegate(this);    
			delegate.getRides();
		}
		
		/**
		 * Don't feel good about having knowledge of the result set here. Check 
		 * out AIRCairngorm's approach for wrapping up that knowledge.
		 * 
		 */
		public function result( data : Object ) : void
		{
		  var sqlEvent:SQLEvent = data as SQLEvent;
		  var result:SQLResult = sqlEvent.target.getResult();
			
			if (result.data != null)
      {
        var rides:ArrayCollection = new ArrayCollection();
        var ride:RideVO = null;
        
        for (var i:int = 0; i < result.data.length; i++)
        {
          ride = new RideVO();
          ride.id = result.data[i].id;
          ride.ride_on = new Date(result.data[i].ride_on);
          ride.distance = result.data[i].distance;
          ride.bike_id = result.data[i].bike_id;
          ride.route_id = result.data[i].route_id;
          ride.average_speed = result.data[i].average_speed;
          ride.max_speed = result.data[i].max_speed;
          ride.riding_time = result.data[i].riding_time;
          ride.elevation_gain = result.data[i].elevation_gain;
          ride.comments = result.data[i].comments;
          ride.bike = ValueObjectUtil.getBikeById(result.data[i].bike_id);
          ride.route = ValueObjectUtil.getRouteById(result.data[i].route_id);
          rides.addItem(ride);
        }
        model.rides = rides;
      }
		}
		
		public function fault( info : Object ) : void
		{
	    if (model.databaseInitialized) Alert.show("Rides could not be retrieved!");
		}
	}
}